/*
 * File Name：PayUtil.java
 * Copyright：Copyright 2008-2014 CiWong.Inc. All Rights Reserved.
 * Description： PayUtil.java
 * Modify By：jejun
 * Modify Date：2014-4-14
 * Modify Type：Add
 */
package com.ciwong.mobilepay.util;

import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.Hashtable;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.NinePatch;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.NinePatchDrawable;
import android.graphics.drawable.StateListDrawable;
import android.text.TextUtils;

import com.ciwong.mobilepay.bean.PayDetail;
import com.ciwong.mobilepay.ui.EMainActivity;
import com.ciwong.mobilepay.view.EPayMethodContainer;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

/**
 * 支付工具类
 * 
 * @author jejun
 * @version ciwong v.1.0 2014-4-14
 * @since ciwong v.1.0
 */
public class PayUtil
{
    /**
     * 启动支付模块
     * 
     * @param mActivity
     *            {@link Activity}
     * @param payDetail
     *            支付详细信息{@link PayDetail}
     * @param requestCode
     *            请求码{@link Activity#startActivityForResult(Intent, int)}
     */
    public static void startPay(Activity mActivity, PayDetail payDetail,
            int requestCode)
    {
        Intent mIntent = new Intent(mActivity, EMainActivity.class);
        mIntent.putExtra(EMainActivity.PAY_DETAIL, payDetail);
        mIntent.putExtra(EMainActivity.GO_BACK, EMainActivity.SHOW_GOBACK_ID);
        mActivity.startActivityForResult(mIntent, requestCode);
    }

    /**
     * 保留小数点
     * 
     * @param value
     *            输入值
     * @param point
     *            保留的小数点
     * @return
     */
    public static String radixPoint(double value, int point)
    {
        String str = "";
        for (int i = 0; i < point; i++)
        {
            if (i == 0)
            {
                str += ".";
            }
            str += "0";
        }
        str = "##0" + str;
        DecimalFormat fnum = new DecimalFormat(str);
        String dd = fnum.format(value);
        return dd;
    }

    /**
     * 获取图片选择器
     * 
     * @param states
     *            状态
     * @param statesDrawables
     *            状态对应图片
     * @return 选择器Drawable对象
     */
    public static StateListDrawable getDrawableSelector(int[][] states,
            Drawable[] statesDrawables)
    {
        int len = states == null ? 0 : states.length;
        if (statesDrawables == null || statesDrawables.length != len)
        {
            return null;
        }
        StateListDrawable selector = new StateListDrawable();
        for (int i = 0; i < len; i++)
        {
            Drawable drawable = statesDrawables[i];
            selector.addState(states[i], drawable);
        }
        return selector;
    }

    /**
     * 获取jar文件图片资源
     * 
     * @param path
     *            文件目录
     * @param clazz
     *            class
     * @return Drawable
     */
    public static Drawable getDrawableFromJar(String path, Class<?> clazz)
    {
        return getDrawableFromJar(path, clazz, new Rect(0, 0, 0, 0));
    }
    /**
     * 根据字符串创建一个二维码图片
     *
     * @param text      需要创建二维码的文字
     * @param QR_WIDTH  输出图片的宽
     * @param QR_HEIGHT 输出图片的高
     * @return 二维码图片
     */
    public static Bitmap createQRcodeBitmap(String text, int QR_WIDTH, int QR_HEIGHT) {
        try {

            if (TextUtils.isEmpty(text)) {
                return null;
            }

            Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            BitMatrix bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints);
            int[] pixels = new int[QR_WIDTH * QR_HEIGHT];
            for (int y = 0; y < QR_HEIGHT; y++) {
                for (int x = 0; x < QR_WIDTH; x++) {
                    if (bitMatrix.get(x, y)) {
                        pixels[y * QR_WIDTH + x] = 0xff000000;
                    } else {
                        pixels[y * QR_WIDTH + x] = 0xffffffff;
                    }
                }
            }

            Bitmap bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT, Bitmap.Config.ARGB_8888);

            bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);
            return bitmap;
        } catch (WriterException e) {
            e.printStackTrace();
            return null;

        }
    }
    /**
     * 
     * 获取jar文件图片资源
     * 
     * @param path
     *            文件目录
     * @param clazz
     *            class
     * @param padding
     *            Rect
     * @return Drawable
     */
    public static Drawable getDrawableFromJar(String path, Class<?> clazz,
            Rect padding)
    {
        InputStream inputstream = clazz.getResourceAsStream(path);
        if (inputstream != null)
        {
            Bitmap localBitmap = BitmapFactory.decodeStream(inputstream);
            return getNinePathDrawable(localBitmap, padding);
        }
        return null;
    }

    /**
     * 处理点九图片Bitmap
     * 
     * @param localBitmap
     * @param padding
     * @return Drawable
     */
    public static Drawable getNinePathDrawable(Bitmap localBitmap, Rect padding)
    {
        if (localBitmap != null)
        {
            byte[] chunk = localBitmap.getNinePatchChunk();
            boolean result = NinePatch.isNinePatchChunk(chunk);
            if (result)
            {
                NinePatchDrawable patchy = new NinePatchDrawable(localBitmap,
                        chunk, padding, null);
                patchy.setCallback(null);
                return patchy;
            }
        }
        return new BitmapDrawable(localBitmap);
    }

}
